package cz.muni.fi.pa165.rentalofconstructionmachinery.dao.impl;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dao.RentalDAO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Customer;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Machine;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Rental;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author David Domonkos
 */
@Repository(value = "rentalDao")
public class RentalDAOImpl implements RentalDAO {
    
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional
    public void createRental(Rental rental) {
        if (rental == null) {
            throw new IllegalArgumentException("'rental' must not be null");
        }
        
        entityManager.persist(rental);
    }

    @Override
    @Transactional
    public void deleteRental(Rental rental) {
        if (rental == null) {
            throw new IllegalArgumentException("'rental' must not be null");
        }
        
        entityManager.remove(entityManager.merge(rental));
    }

    @Override
    @Transactional
    public void updateRental(Rental rental) {
        if (rental == null) {
            throw new IllegalArgumentException("'rental' must not be null");
        }
        if (getRentalById(rental.getId()) == null) {
            throw new IllegalArgumentException("Rental must be first created before update");
        }
        
        entityManager.merge(rental);
    }

    @Override
    public List<Rental> getAllRentals() {
        List<Rental> rentals = entityManager.createQuery("SELECT r FROM Rental r", Rental.class).getResultList();
        return rentals;
    }

    @Override
    public Rental getRentalById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("'id' must not be null");
        }
        
        Rental rental = entityManager.createQuery("SELECT r FROM Rental r WHERE r.id=" + id, Rental.class).getSingleResult();
        return rental;
    }
    
    @Override
    public List<Rental> getRentalsByCustomer(Customer customer) {
        if (customer == null) {
            throw new IllegalArgumentException("'customer' must not be null");
        }
        
        List<Rental> rentals = entityManager.createQuery("SELECT r FROM Rental r WHERE customerId=" + customer.getId(), Rental.class).getResultList();
        return rentals;
    }
    
    @Override
    public List<Rental> getRentalsByMachine(Machine machine) {
        if (machine == null) {
            throw new IllegalArgumentException("'machine' must not be null");
        }
        
        List<Rental> rentals = entityManager.createQuery("SELECT r FROM Rental r WHERE machineId=" + machine.getId(), Rental.class).getResultList();
        return rentals;
    }
    
}
